package cn.com.gridinfo.sooc.core.expand.score.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.uiUtils.ScrollSmoothLineaerLayoutManager;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.score.adapter.MyInviteAdapter;
import cn.com.gridinfo.sooc.core.expand.score.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.score.dao.ScoreDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 我的邀请界面
 *
 * @author yingchunli
 */
public class MyInviteActivity extends BaseSoocActivity {

    @InjectView(R.id.myinvite_list)
    UltimateRecyclerView myinvite_list;//信息的列表
    @InjectView(R.id.toolbar)
    Toolbar toolbar;//toolbar
    @InjectView(R.id.yaoqingma_text)
    TextView yaoqingma;//邀请码
    @InjectView(R.id.blankTip)
    TextView blankTip;//空白页提示
    @InjectView(R.id.count_ren)
    TextView countRen;//邀请人数，通过列表数量计算
    private MyInviteAdapter adapter;//适配器
    LinearLayoutManager linearLayoutManager;//布局管理器
    ScoreDao dao;
    int maxLastPositon;//分页最后位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_score_myinvite);
        ButterKnife.inject(this);
        dao = new ScoreDao(this);

        //设置toolbar背景
        String release = android.os.Build.VERSION.RELEASE;  // android系统版本号
        if (release.startsWith("4")) {
            //5.0以下的系统
            toolbar.setBackgroundResource(R.mipmap.expand_myinvite_toolbar_bg_small);
        } else {
            toolbar.setBackgroundResource(R.mipmap.expand_myinvite_toolbar_bg);
        }

        yaoqingma.setText(Arad.preferences.getString(LocalMark.USER_UID.getName()));//邀请码
        //初始化listView
        linearLayoutManager = new LinearLayoutManager(this);
        myinvite_list.setLayoutManager(linearLayoutManager);

        //发送请求，获取我的邀请列表内容
        PageConst.pageNo = 1;
        PageConst.pageSize=20;
        dao.getMyInviteList(PageConst.pageNo, PageConst.pageSize, CodeConst.GET_MYINVITE_FIRST);
    }

    //请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.GET_MYINVITE_FIRST:
                        //获取我的邀请列表成功  首次请求
                        initView();
                        if (dao.getMyInviteList() != null && !dao.getMyInviteList().isEmpty()) {
                            countRen.setText(dao.getCount() + "");//设置邀请人数
                            blankTip.setVisibility(View.GONE);
                        } else {
                            //无数据时，显示提示
                            blankTip.setVisibility(View.VISIBLE);
                            countRen.setText("0");//设置邀请人数
                        }
                        break;
                    case CodeConst.GET_MYINVITE_PULLUP://获取我的邀请列表   上拉请求时
                        if (dao.getMyInviteList() != null && !dao.getMyInviteList().isEmpty()) {
                            adapter.loadmore(dao.getMyInviteList());
                        } else {
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPosition(maxLastPositon - 1);
                        }
                        break;
                    case CodeConst.GET_MYINVITE_PUSHDOWN://获取我的邀请列表  下拉请求时
                        myinvite_list.setRefreshing(false);
                        if (dao.getMyInviteList() != null && !dao.getMyInviteList().isEmpty()) {
                            countRen.setText(dao.getCount() + "");//设置邀请人数
                            blankTip.setVisibility(View.GONE);
                        } else {
                            Log.d("无数据时");
                            blankTip.setVisibility(View.VISIBLE);
                        }
                        adapter.refresh(dao.getMyInviteList());
                        if (dao.getMyInviteList().size() > (PageConst.pageSize - 1)) {
                            adapter.loadMoreDataView();
                        } else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }


    public void initView() {
        adapter = new MyInviteAdapter(dao.getMyInviteList(), this);
        myinvite_list.setAdapter(adapter);
        myinvite_list.enableLoadmore();
        myinvite_list.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //下拉刷新
                PageConst.pageNo = 1;
                dao.getMyInviteList(PageConst.pageNo, PageConst.pageSize, CodeConst.GET_MYINVITE_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        myinvite_list.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                //上拉加载
                Log.i("itemsCount->" + itemsCount + "");
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.getMyInviteList(++PageConst.pageNo, PageConst.pageSize, CodeConst.GET_MYINVITE_PULLUP);
                } else {
                    adapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
    }

    @Override
    public String setupToolBarTitle() {
        return "我的邀请";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

}
